import { WeaSearchGroup, WeaFormItem, WeaTools,WeaUpload } from "ecCom";
import { Button } from "antd";
import { toJS } from "mobx";
import { WeaSwitch } from "comsMobx";
import { observer } from "mobx-react";
import _ from "lodash";
var Base64 = ecCom.WeaTools.Base64;

@observer
export default class FormBottom extends React.Component {
  constructor(props) {
    super(props);
  }
  componentDidMount() { }
  componentDidUpdate() {
    const { listStore2 } = this.props;
    if(listStore2){
      listStore2.getuploadTaskRef(this.refs);
    }
  }
  render() {
    const { condition } = this.props.listStoreB.listStoreB;
    const { formB, col } = this.props;
    const { isFormInit } = formB;
    let suffix;//标识。防止上传组件数据错乱
    let group = [];
    isFormInit &&
      toJS(condition).map((c, index) => {
        let items = [];
        c.items = c.items || [];
        c.items.map((field, index) => {
          suffix = field.domkey[0];
          items.push({
            com: (
              <div className="govern-dialog-form-item">
                <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@j0qign@${suffix}`}
                  label={`${field.label}`}
                  labelCol={{ span: `${field.labelcol}` }}
                  error={(field.viewAttr == 3 && formB.getError(field))}//必填提示
                  tipPosition="bottom"
                  wrapperCol={{ span: `${field.fieldcol}` }}
                >
                  {/*  <WeaSwitch fieldConfig={field} form={formB} />  */}
                  {/*(field.formItemType == "RICHTEXT" && _.has(field, 'value') && field.value != "" && field.viewAttr == 1) ? <div style={{ width: 500 }} dangerouslySetInnerHTML={{ __html: Base64.decode(field.value) }}></div> : <WeaSwitch fieldConfig={field} form={formB} />*/}
                  {
                    this.getFormItem(field, formB)
                  }
                </WeaFormItem>
              </div>
            ),
            col: col || field.colSpan
          });
        });
        group.push(
          <div className="govern-add-dialog-bottom" key={suffix}>
            <div className="number">
              <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@8e7uv5@${suffix}`} shape="circle" type="dashed">{index + 1}</Button>
            </div>
            <div className="itemBtns">
              <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@w1y754@${suffix}`}
                type="primary"
                shape="circle"
                icon="plus"
                onClick={this.btnAddClick.bind(this, index)}
              />
              {/*  <Tooltip
                title="已是最后一项"
                visible={visibleTip}
                mouseLeaveDelay={0.1}
              > */}
              <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@fy7w56@${suffix}`}
                type="danger"
                shape="circle"
                icon="delete"
                onClick={this.btnDelClick.bind(this, index)}
              />
              {/* </Tooltip> */}
            </div>
            <WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@15b7hq@${suffix}`}
              needTigger={true}
              showGroup={c.defaultshow}
              items={items}
              style={{ background: "#F9F9F9" }}
            />
          </div>
        );
      });
    return group;
  }
  btnAddClick(index) {
    const { btnAddClick } = this.props.listStoreB;
    btnAddClick(index);
  }
  btnDelClick(index) {
    const { btnDelClick } = this.props.listStoreB;
    btnDelClick(index);
  }

  getFormItem(field, form) {
    let dom;
    if (field.formItemType == "RICHTEXT" && _.has(field, 'value') && field.value != "" && field.viewAttr == 1)
      dom = this.richTextItem(field);
    else if (field.conditionType == "UPLOAD")
      dom = this.getUploadItem(field, form)
    else
      dom = <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@nykbnh`} fieldConfig={field} form={form} />
    return dom;
  }

  richTextItem(field) {
      return  <div style={{ width: 500 }} dangerouslySetInnerHTML={{ __html: Base64.decode(field.value) }}></div>
  }

  //获取上传组件
  getUploadItem(field, form) {
    const {listStore2} = this.props;
    let { uploadSize, updateForm,submitFlag } = listStore2;
    let upload = {};
    return <WeaUpload ecId={`${this && this.props && this.props.ecId || ''}_WeaUpload@ywbdih`}
      ref={field.domkey[0]}
      viewAttr={field.viewAttr}
      uploadUrl={field.uploadUrl}
      category={field.category}
      autoUpload={field.otherParams.autoUpload}
      datas={field.datas}
      multiSelection={field.multiSelection}
      maxUploadSize={field.otherParams.maxUploadSize}
      getShowListDatas={(listT, listB) => {
        if (listB.length > 0 && listT == 0 && !submitFlag) {
          upload[field.domkey] = "xk";
          form.updateFields(upload);//赋值绕过检验规则
        }
        updateForm(listT, listB, upload, form, field, uploadSize);
      }}
    >
    </WeaUpload>
  }
}
